<?php
namespace Modules\Home\Validations;

use Phalcon\Validation\Validator\Digit;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\InclusionIn;

class GoodsValidation extends \Phalcon\Validation {
  public function initialize() {
    $this -> add('safe_password',
      new PresenceOf([
        'message' => '请输入安全密码!'
      ])
    );
  }

  public function transfer($data) {
    $sessionGetUser = $this -> session -> get('user');

    if($data['user_code'] == $sessionGetUser['user_code']) {
      return ['code' => -1, 'message' => '不能转账给同一用户'];
    }

    $this -> add('user_code', new PresenceOf(['message' => '请输入用户编号!']));

    $this -> add('type', new InclusionIn(['message' => '请选择正确类型!', 'domain' => [4, 41]]));

    $this -> rules('num',[
      new PresenceOf(['message' => '请输入数量!']),
      new Digit(['message' => '请输入正确数字'])
    ]);

    $validationMessageGroup = $this -> validate($data);
    if($validationMessageGroup -> valid()) {
      return ['code' => -1, 'message' => $validationMessageGroup[0] -> getMessage()];
    }

    return ['code' => 0]; 
  }
}
?>